package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.StrategyCommend;
import cn.wolfcode.luowowo.article.domain.TravelCommend;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.ITravelCommendService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.*;
import cn.wolfcode.luowowo.search.template.DestinationTemplate;
import cn.wolfcode.luowowo.search.template.StrategyTemplate;
import cn.wolfcode.luowowo.search.template.TravelTemplate;
import cn.wolfcode.luowowo.search.template.UserInfoTemplate;
import cn.wolfcode.luowowo.search.vo.SearchResultVO;
import cn.wolfcode.luowowo.website.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.util.CookieUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
public class IndexController {
    @Reference
    private IUserInfoRedisService userInfoRedisService;
    @Reference
    private ITravelCommendService commendService;
    @Reference
    private IStrategyCommendService strategyCommendService;
    @Reference
    private ITravelService travelService;
    @Reference
    private IDestinationTemplateSearchService destinationTemplateSearchService;
    @Reference
    private IStrategyTemplateSearchService strategyTemplateSearchService;
    @Reference
    private ITravelTemplateSearchService travelTemplateSearchService;
    @Reference
    private IUserInfoTemplateSearchService userInfoTemplateSearchService;
    @Reference
    private ISearchService searchService;
    @RequestMapping("")
    public String userLogin(Model model){
        //tcs
        List<TravelCommend> tcs = commendService.getTop5();
        model.addAttribute("tcs",tcs);
        //sc 一篇
        StrategyCommend commend = strategyCommendService.getTop5().get(0);
        model.addAttribute("sc",commend);
        return "index/index";
    }
    @RequestMapping("index/travelPage")
    public String travel(Model model, @ModelAttribute("qo")TravelQuery qo){
        PageInfo pageInfo= travelService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "index/travelPageTpl";
    }
    @RequestMapping("/q")
    public String search(Model model, @ModelAttribute("qo") SearchQueryObject qo) {
        switch (qo.getType()) {
            case SearchQueryObject.SEARCH_TYPE_DEST://0
                return searchDest(model, qo);
            case SearchQueryObject.SEARCH_TYPE_STRATEGY://1
                return SearchStrategy(model, qo);
            case SearchQueryObject.SEARCH_TYPE_TRAVEL://2
                return searchTravel(model, qo);
            case SearchQueryObject.SEARCH_TYPE_USER://3
                return searchUser(model, qo);
            default:
                return searchAll(model, qo);
        }
    }

    private String searchDest(Model model, SearchQueryObject qo) {
        SearchResultVO data = new SearchResultVO();
        DestinationTemplate dest = destinationTemplateSearchService.findByName(qo.getKeyword());
        model.addAttribute("dest", dest);
        if (dest != null) {
            //StrategyTemplate
            List<StrategyTemplate> sts = strategyTemplateSearchService.getByDestName(qo.getKeyword());
            data.setTotal(sts.size() + data.getTotal());
            if (sts.size() > 5) {
                sts.subList(0, 5);
            }
            List<TravelTemplate> ts = travelTemplateSearchService.getByDestName(qo.getKeyword());
            data.setTotal(ts.size() + data.getTotal());
            if (ts.size() > 5) {
                ts.subList(0, 5);
            }
            List<UserInfoTemplate> us = userInfoTemplateSearchService.getByDestName(qo.getKeyword());
            data.setTotal(us.size() + data.getTotal());
            if (us.size() > 5) {
                us.subList(0, 5);
            }
            data.setStrategys(sts);
            data.setTravels(ts);
            data.setUsers(us);
        }
        model.addAttribute("data", data);
        //Page<DestinationTemplate> page = searchService.searchWithHighlight(DestinationTemplate.INDEX_NAME, DestinationTemplate.TYPE_NAME, DestinationTemplate.class, qo, "name", "info");
        //model.addAttribute("page",page);
        return "index/searchDest";
    }

    private String searchAll(Model model, SearchQueryObject qo) {
        Page<StrategyTemplate> sts = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME, StrategyTemplate.class, qo, "title", "subTitle", "sumary");
        Page<UserInfoTemplate> us = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME, UserInfoTemplate.class, qo, "nickname", "destName");
        Page<TravelTemplate> ts = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME, TravelTemplate.class, qo, "title", "sumary");
        Page<DestinationTemplate> ds = searchService.searchWithHighlight(DestinationTemplate.INDEX_NAME, DestinationTemplate.TYPE_NAME, DestinationTemplate.class, qo, "name", "info");
        SearchResultVO data = new SearchResultVO();
        data.setTotal(ds.getTotalElements() + sts.getTotalElements() + ts.getTotalElements() + us.getTotalElements());
        data.setUsers(us.getContent());
        data.setTravels(ts.getContent());
        data.setStrategys(sts.getContent());
        data.setDests(ds.getContent());
        model.addAttribute("data", data);

        return "index/searchAll";
    }

    //用户
    private String searchUser(Model model, SearchQueryObject qo) {
        Page<UserInfoTemplate> page = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME, UserInfoTemplate.class, qo, "nickname", "destName");
        model.addAttribute("page", page);
        return "index/searchUser";
    }

    //游记
    private String searchTravel(Model model, SearchQueryObject qo) {
        Page<TravelTemplate> page = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME, TravelTemplate.class, qo, "title", "sumary");
        model.addAttribute("page", page);
        return "index/searchTravel";
    }

    //攻略
    private String SearchStrategy(Model model, SearchQueryObject qo) {
        Page<StrategyTemplate> page = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME, StrategyTemplate.class, qo, "title", "subTitle", "sumary");
        model.addAttribute("page", page);
        return "index/searchStrategy";
    }


}